#include "widget.h"
#include "./ui_widget.h"

#include <QPainter>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    startAngle = 0;

    // 定时器
    timer = new QTimer(this);

    // 连接timer的信号与槽
    connect(timer, &QTimer::timeout, [=]() {
        // 更新开始的角度
        startAngle += 3;
        startAngle %= 360;
        update();
    });

    // 每隔100毫秒产生一个信号timeout
    timer->setInterval(50);
    timer->start();
}

Widget::~Widget()
{
    delete ui;
}


void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    int r; // 半径

    QBrush brush(Qt::black);
    painter.setBrush(brush);
    painter.drawRect(rect());

    painter.translate(rect().center()); // 坐标原点转换到屏幕中央

    r = (height() > width() ? width() : height()) / 2 / 7;

    QPen pen(Qt::green);
    painter.setPen(pen);
    painter.setBrush(Qt::NoBrush);

    for (int i = 1; i <= 7; i++) { // 7个同心圆
        painter.drawEllipse(QPoint(0, 0), i * r, i * r);
    }

    //painter.setPen(QPen(Qt::red));
    painter.drawLine(-r * 7, 0 , r * 7, 0);
    painter.drawLine(0, -r * 7, 0, r * 7);

    QConicalGradient conical(0, 0, -startAngle);
    conical.setColorAt(0, QColor(0, 255, 0, 100));
    conical.setColorAt(1, QColor(0, 0, 0, 100));
    conical.setColorAt(0.0, QColor(0, 255, 0, 90));
    conical.setColorAt(1.0, QColor(0, 255, 0, 0));

    painter.setBrush(conical);
    painter.setPen(Qt::NoPen);
    painter.drawPie(-r * 7, -r * 7, r * 7 * 2, r * 7 * 2, -startAngle * 16, 150 * 15);
}

